package com.unytech.wisdomcommunity.complainrepairfragment;

import android.app.Fragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.unytech.wisdomcommunity.R;

/**
 * Created by zed on 2016/11/24/0024.
 * 因UI对原有"投诉报修"不满意,修改之后的activity(二稿)
 */
public class BaseAvtivity2 extends AppCompatActivity {
    private TextView tv_comlain, tv_repair;
    private TextView mTitleTv;
    private ImageView comlainOrRepairIv,comlainRepairBackIv;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_fragment2);

        getFragmentManager()
                .beginTransaction()
                .replace(R.id.demo_base_fragment_container2, new ComplainFragment2())
                .commit();
        //初始化view
        initView();
    }

    private void initView() {
        mTitleTv = ((TextView) findViewById(R.id.comlainrepair_fragment_base_title_tv2));
        mTitleTv.setText("投诉");

        comlainRepairBackIv = ((ImageView) findViewById(R.id.comlainrepair_fragment_base_back_iv2));
        comlainRepairBackIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        comlainOrRepairIv = ((ImageView) findViewById(R.id.comlain_and_repair_change_iv2));
        comlainOrRepairIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //2.初始化popupWindow
                initPopupWindow(v);
            }
        });
    }

    private void initPopupWindow(View v) {
        View pupView = LayoutInflater.from(this).inflate(R.layout.popup_window_comlain_repair_layout, null);

        final PopupWindow popupWindow = new PopupWindow(pupView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);

        tv_comlain = ((TextView) pupView.findViewById(R.id.pop_tv_complain));
        tv_repair = ((TextView) pupView.findViewById(R.id.pop_tv_repair));

        tv_comlain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new ComplainFragment2());//替换fragment
                mTitleTv.setText("投诉");
                popupWindow.dismiss();
            }
        });
        tv_repair.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new RepairFragment2());//替换fragment
                mTitleTv.setText("报修");
                popupWindow.dismiss();
            }
        });

        /*popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);*/
        //获取view 在屏幕上的X和Y的坐标   location[0]表示X轴   location[1]表示Y轴
        int[] location = new int[2];
        v.getLocationOnScreen(location);

        popupWindow.setOutsideTouchable(true);
        //popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        //把事件拦截下来
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
        popupWindow.showAtLocation(comlainOrRepairIv, Gravity.TOP|Gravity.RIGHT,location[0] -1300, location[1]+150);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //backgroundAlpha(BaseActivity.this, 1f);
                //把这个方法注释掉  解决弹出popupWindow时屏幕闪一下黑屏的问题
            }
        });
    }
    //替换fragment的方法
    private void replaceFragment(Fragment fragment) {
        getFragmentManager()
                .beginTransaction()
                .replace(R.id.demo_base_fragment_container2, fragment)
                .commit();
    }
}
